package com.zh.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.constants.Constants;
import com.zh.pojo.bo.PayResultBo;
import com.zh.pojo.domain.Order;
import com.zh.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = Constants.MQ.CONSUMER_GROUP_PAY_ORDER_RESULT,
        topic = Constants.MQ.TOPIC_PAY_RESULT,
        selectorExpression = Constants.MQ.TAGS_PAY_RESULT)
public class PayOrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IOrderService orderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String message = new String(body, StandardCharsets.UTF_8);
        PayResultBo payResultBo = JSON.parseObject(message,PayResultBo.class);
        log.info("支付结果处理：{}", payResultBo);
        // 幂等处理 防止重复消费   保证同一笔订单只被消费一次  使用 分布式锁
        orderService.updateOrderStatus(payResultBo);

    }
}
